package cn.donting.jme.expansion.builder.spatial;


import ch.qos.logback.classic.spi.EventArgUtil;
import cn.donting.jme.EditorContent;
import cn.donting.jme.editor.scene.Scene;
import cn.donting.jme.expansion.attribute.Attribute;
import cn.donting.jme.expansion.attribute.ValueChangeListener;
import cn.donting.jme.expansion.attribute.lmpl.Vector3fAttribute;
import cn.donting.jme.expansion.builder.AttributeBuilder;

import cn.donting.jme.ioc.Bean;
import cn.donting.jme.utile.MathUtil;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Transform;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;

import java.util.List;

@Bean
public class TransformAttributeBuilder implements AttributeBuilder<Transform> {
    public static final String TRANSLATION_ATTRIBUTE_KEY = "translation";
    public static final String ROTATION_ATTRIBUTE_KEY = "rotation";
    public static final String SCALE_ATTRIBUTE_KEY = "scale";
    private final ThreadLocal<Spatial> spatialThreadLocal=new ThreadLocal<>();

    public void setSpatial(Spatial spatial){
        spatialThreadLocal.set(spatial);
    }
    public void removeSpatial(){
        spatialThreadLocal.remove();
    }
    @Override
    public boolean isHandle(Object o) {
        return o instanceof Spatial;
    }

    @Override
    public void attribute(List<Attribute> attributes, Transform transform) {
        Spatial spatial = spatialThreadLocal.get();
        Vector3fAttribute translationAttribute = new Vector3fAttribute();
        Vector3f translation = transform.getTranslation();
        translationAttribute.setTitle(TRANSLATION_ATTRIBUTE_KEY);
        translationAttribute.setKey(TRANSLATION_ATTRIBUTE_KEY);
        translationAttribute.setValue(translation);
        translationAttribute.setValueChangeListener(newValue -> {
            EditorContent.runJme(() -> {
                spatial.setLocalTranslation(newValue);
            });

        });
        attributes.add(translationAttribute);


        Vector3fAttribute rotationAttribute = new Vector3fAttribute();
        Quaternion rotation = transform.getRotation();
        Vector3f vector3f = MathUtil.quaternionToAngles(rotation);
        rotationAttribute.setValue(vector3f);
        rotationAttribute.setValueChangeListener((newValue) -> {
            EditorContent.runJme(() -> {
                Quaternion quaternion = MathUtil.anglesToQuaternion(newValue);
                spatial.setLocalRotation(quaternion);
            });

        });
        rotationAttribute.setTitle("rotation");
        rotationAttribute.setKey(ROTATION_ATTRIBUTE_KEY);
        attributes.add(rotationAttribute);


        Vector3fAttribute scaleAttribute = new Vector3fAttribute();
        Vector3f scale = transform.getScale();
        scaleAttribute.setTitle("scale");
        scaleAttribute.setKey(SCALE_ATTRIBUTE_KEY);
        scaleAttribute.setValue(scale);
        scaleAttribute.setValueChangeListener(newValue -> {
            EditorContent.runJme(() -> {
                spatial.setLocalScale(newValue);
            });
        });

        attributes.add(scaleAttribute);
    }

}
